package com.lx.algorithm.mergeSort;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-05 03:18:32
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-05     张李鑫                     1.0         1.0 Version
 */
public class BiggerThanRightTwice {


    public static int process(int[] arr) {
        if (null == arr || arr.length < 2) {
            return 0;
        }
        return process(arr, 0, arr.length - 1);
    }


    public static int process(int[] arr, int left, int right) {
        if (left == right) {
            return 0;

        }
        int mid = left + ((right - left) >> 1);
        return process(arr, left, mid) + process(arr, mid + 1, right) + merge(arr, left, mid, right);
    }


    /**
     * <p>
     *    比较左右组的数(前提是 两个组已经保证有序了)
     *    两个数组从小开始遍历 如果左边数小于等于右边的数 就放到待排序数组中
     *    如果大于右边数组的数 count+ 右边数组当前索引到开始索引的个数 就是当前大于的个数
     * </p>
     * @param  arr
     * @param  left
     * @param  mid
     * @param  right
     * @return int
     * @Author zlx
     * @date 2021/9/5
     */
    public static int merge(int[] arr, int left, int mid, int right) {

        int p1 = left;
        int p2 = mid+1;
        int count = 0;
        int[] mergeArr = new int[right - left + 1];
        int i = 0;
        for (int i1 = p1; i1 <= mid; i1++) {
            while (p2<=right&&arr[i1]>arr[p2]*2){
                p2++;
            }
            count+=p2-mid-1;
        }
         p1 = left;
         p2 = mid+1;

        while (p1 <= mid && p2 <= right) {

            mergeArr[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];
        }
        while (p1 <=mid) {
            mergeArr[i++] = arr[p1++];
        }
        while (p2 <= right) {
            mergeArr[i++] = arr[p2++];
        }

        for (int i1 = 0; i1 < mergeArr.length; i1++) {
            arr[left++] = mergeArr[i1];
        }
        return count;
    }


    // for test
    public static int comparator(int[] arr) {
        int ans = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > (arr[j] << 1)) {
                    ans++;
                }
            }
        }
        return ans;
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) ((maxValue + 1) * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            if (process(arr1) != comparator(arr2)) {
                System.out.println("Oops!");
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println("测试结束");

    }



}